package com.zbkj.admin.controller.platform;

import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.DeliveryPersonnel;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.DeliveryPersonnelRequest;
import com.zbkj.common.request.DeliveryPersonnelSearchRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.DeliveryPersonnelService;
import com.zbkj.service.service.SystemAdminService;
import com.zbkj.service.service.SystemAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 配送员表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/platform/delivery/personnel")
@Api(tags = "配送员控制器")
public class DeliveryPersonnelController {

    @Autowired
    private DeliveryPersonnelService deliveryPersonnelService;
    @Autowired
    private SystemAttachmentService systemAttachmentService;


    /**
     * 分页显示配送员表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author keNan
     * @since 2025-03-12
     */
    @PreAuthorize("hasAuthority('platform:delivery:personnel:list')")
    @ApiOperation(value = "分页列表") //配合swagger使用
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<DeliveryPersonnel>> getList(@Validated DeliveryPersonnelSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<DeliveryPersonnel> deliveryPersonnelCommonPage = CommonPage.restPage(deliveryPersonnelService.getList(request, pageParamRequest));
        return CommonResult.success(deliveryPersonnelCommonPage);
    }

    /**
     * 新增配送员表
     *
     * @param deliveryPersonnelRequest 新增参数
     * @author keNan
     * @since 2025-03-12
     */
    @PreAuthorize("hasAuthority('platform:delivery:personnel:save')")
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated DeliveryPersonnelRequest deliveryPersonnelRequest) {
        DeliveryPersonnel deliveryPersonnel = new DeliveryPersonnel();
        BeanUtils.copyProperties(deliveryPersonnelRequest, deliveryPersonnel);
        deliveryPersonnel.setCreateTime(new Date());
        deliveryPersonnel.setFrontOfIdCard(systemAttachmentService.clearPrefix(deliveryPersonnelRequest.getFrontOfIdCard()));
        deliveryPersonnel.setBackOfIdCard(systemAttachmentService.clearPrefix(deliveryPersonnelRequest.getBackOfIdCard()));
        deliveryPersonnelService.check(deliveryPersonnelRequest);

        if (deliveryPersonnelService.save(deliveryPersonnel)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除配送员表
     *
     * @param id Integer
     * @author keNan
     * @since 2025-03-12
     */
    @PreAuthorize("hasAuthority('platform:delivery:personnel:delete')")
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        if (deliveryPersonnelService.removeById(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改配送员表
     *
     * @param deliveryPersonnelRequest 修改参数
     * @author keNan
     * @since 2025-03-12
     */
    @PreAuthorize("hasAuthority('platform:delivery:personnel:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated DeliveryPersonnelRequest deliveryPersonnelRequest) {
        if (null == deliveryPersonnelRequest.getId()) {
            throw new CrmebException("id不能为空");
        }
        DeliveryPersonnel deliveryPersonnel = new DeliveryPersonnel();
        BeanUtils.copyProperties(deliveryPersonnelRequest, deliveryPersonnel);

        deliveryPersonnelService.check(deliveryPersonnelRequest);
        deliveryPersonnel.setFrontOfIdCard(systemAttachmentService.clearPrefix(deliveryPersonnelRequest.getFrontOfIdCard()));
        deliveryPersonnel.setBackOfIdCard(systemAttachmentService.clearPrefix(deliveryPersonnelRequest.getBackOfIdCard()));
        if (deliveryPersonnelService.updateById(deliveryPersonnel)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }


}



